package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmy.cultivate.entity.*;
import com.xmy.cultivate.service.*;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 续报成交数据 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2023-06-17
 */
@RestController
@RequestMapping("/api/dealInfo")
@Scope("prototype")
public class DealInfoController {

    @Autowired
    IDealInfoService iDealInfoService;

    @Autowired
    IStaffService iStaffService;

    @Autowired
    IEnrollInfoService iEnrollInfoService;


    @GetMapping("/getDealInfoList/{orderId}")
    public Result getDealInfoList(@PathVariable(value = "orderId") String orderId){
        QueryWrapper<DealInfo> dealInfoQueryWrapper = new QueryWrapper<>();
        dealInfoQueryWrapper.eq("order_id",orderId);
        List<DealInfo> dealInfoList = iDealInfoService.list(dealInfoQueryWrapper);
        for (DealInfo dealInfo:dealInfoList){
            Staff staff = iStaffService.getOneById(dealInfo.getTeacherId());
            if(staff != null){
                dealInfo.setTeacherName(staff.getName());
            }
        }

        return new Result(Code.OK,new PageResult(dealInfoList,(long)dealInfoList.size(),1L));
    }

    @DeleteMapping("/delDealInfo/{id}")
    public Result delDealInfo(@PathVariable(value = "id") Long id){
        iDealInfoService.delDealInfo(id);
        return new Result(Code.OK,"","");
    }

    /**
     * 从订单弹窗添加战队成员
     * @param orderApply
     * @return
     */
    @PostMapping("/addEnrollInfo")
    public Result addEnrollInfo(@RequestBody OrderApply orderApply){

        QueryWrapper<EnrollInfo> enrollInfoQueryWrapper = new QueryWrapper<>();
        enrollInfoQueryWrapper.eq("order_id",orderApply.getId());
        Long count = iEnrollInfoService.count(enrollInfoQueryWrapper);
        if (count >0){
            return new Result(Code.ERROR,"","已存在招生信息，不能添加，如要添加，请先删除招生信息");
        }
        iDealInfoService.addDealInfo(orderApply);
        return new Result(Code.OK,"","");
    }

}
